/**
 * Copyright (C) 2013 AirChina
 *
 * @className:com.airchina.wechat.external.util.DateUtil
 * @description:
 * 
 * @version:v1.0.0 
 * @author:larry
 * 
 * Modification History:
 * NO   Date         Author      Version     Description
 * -----------------------------------------------------------------
 *     2013-7-5     larry       v1.0.0        create
 *
 *
 */
package com.china.itapi.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @author:larry
 * @version:v1.0.0
 * @Created:2013-7-5下午2:09:49
 * @Modified:
 */
public class DateUtil {
	public static String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	
	private static SimpleDateFormat aceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
	
	private static SimpleDateFormat checkInsdf= new SimpleDateFormat("yyyyMMdd HHmm");
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
	
	private static SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
	
	private static SimpleDateFormat formatYyyyMmDd= new SimpleDateFormat("yyyyMMdd");
	
	private static Map<String,SimpleDateFormat> formatMap = new HashMap<String,SimpleDateFormat>();
	
	public static String formatDate(Date date, String pattern){
		SimpleDateFormat format = formatMap.get(pattern);
		if(format == null){
			format = new SimpleDateFormat(pattern);
		}
		return format.format(date);
	}
	
	public static Date parseDate(String str, String pattern) throws ParseException {
		SimpleDateFormat format = formatMap.get(pattern);
		if(format == null){
			format = new SimpleDateFormat(pattern);
		}
		return format.parse(str);
	}
	
	public static Date parseAceDate(String str) throws ParseException {
		return aceFormat.parse(str);
	}
	
	public static Date parseCheckInDate(String str) throws ParseException {
		return checkInsdf.parse(str);
	}
	
	public static Date parseYyyyMmDdDate(String str) throws ParseException {
		return formatYyyyMmDd.parse(str);
	}
	
	public static String formatYyyyMmDdDate(Date date) throws ParseException {
		return format.format(date);
	}
	
	/**
	 * 
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description:（yyyy-MM-dd'T'HH:mm:ss.SSS）格式日期获取日期
	  * @param str
	  * @return
	  * @throws ParseException String
	  * @Created:gaoqiang 2013-11-8下午1:21:08
	  * @Modified:
	 */
	public static String str2DateStr(String str) throws ParseException {
		Date date = parseAceDate(str);
		return str2DateStr(date);
	}
	
	/**
	 * 
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description:返回Date对象日期（yyyy-MM-dd）
	  * @param str
	  * @return
	  * @throws ParseException String
	  * @Created:gaoqiang 2013-11-8下午1:21:08
	  * @Modified:
	 */
	public static String str2DateStr(Date date) throws ParseException {
		String dateStr =  dateFormat.format(date);
		return dateStr;
	}
	
	/**
	 * 
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description:（yyyy-MM-dd'T'HH:mm:ss.SSS）格式日期获取时间(HH:mm)
	  * @param str
	  * @return
	  * @throws ParseException String
	  * @Created:gaoqiang 2013-11-8下午1:21:08
	  * @Modified:
	 */
	public static String str2TimeStr(String str) throws ParseException {
		Date date = parseAceDate(str);
		String timeStr =  timeFormat.format(date);
		return timeStr;
	}
	
	/**
	 * 
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description:获取当前日期是星期几
	  * @param dt 日期Date对象
	  * @return String 星期字符串
	  * @Created:gaoqiang 2013-11-8下午1:20:35
	  * @Modified:
	 */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }
	
	/**
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description:验证身份证中的日期是否合法,接受日期格式：20120221
	  * @param date
	  * @return boolean
	  * @Created:CH 2013-10-29上午11:11:41
	  * @Modified:
	 */
	public static boolean verifyDate(String date) {
		String v = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229)";
		Pattern p = Pattern.compile(v);
		Matcher m = p.matcher(date);
		return m.matches();
	}
	
	/**
	 * 
	  * @Description: 
	  * @param dateString 格式：yyyy-MM-dd
	  * @return
	  * @throws ParseException Date
	  * @Created:lining 2013年11月7日下午2:08:45
	  * @Modified:
	 */
	public static Date string2Date(String dateString) throws ParseException {
		return format.parse(dateString);
	}
	
	/**
	 * 
	  * @Description:
	  * @param date
	  * @return String 格式：yyyy-MM-dd
	  * @Created:lining 2013年11月7日下午2:10:57
	  * @Modified:
	 */
	public static String date2String(Date date) {
		return format.format(date);
	}
	
	public static String date2String(Date date, String dateFormat) {
		SimpleDateFormat sdf= new SimpleDateFormat(dateFormat);
		String dateString = sdf.format(date);
		return dateString;
	}
	
	public static Date String2Date(String dateString, String dateFormat){
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date getNow() {
		return new Date();
	}
	
	/**
	 * 
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description:当前日期字符串加指定天数
	  * @param date 原日期
	  * @param day 增加天数
	  * @return String 增加后的日期
	  * @Created:gaoqiang 2013-11-19下午1:48:18
	  * @Modified:
	 */
	public static String addDay(String date , int day){
		
		try {
			Date objDate = format.parse(date);
			Calendar c = Calendar.getInstance();  
			c.setTime(objDate);
			c.add(Calendar.DATE, day);
			Date newDate = c.getTime();
			String newDateStr = format.format(newDate);
			return newDateStr;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String addMonth(String date , int day){
		
		try {
			Date objDate = format.parse(date);
			Calendar c = Calendar.getInstance();  
			c.setTime(objDate);
			c.add(Calendar.MONTH, day);
			Date newDate = c.getTime();
			String newDateStr = format.format(newDate);
			newDateStr = newDateStr.substring(0, 8)+ "01";
			return newDateStr;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	  * @Description: 计算两个日期之间的小时差
	  * @param start
	  * @param end
	  * @return int
	  * @Created:lining 2013年11月14日下午2:42:28
	  * @Modified:
	 */
	public static int dateCompute(Date start, Date end) {
		Calendar c_start = Calendar.getInstance();
		Calendar c_end = Calendar.getInstance();
		c_start.setTime(start);
		c_end.setTime(end);
		int result = c_end.get(Calendar.HOUR_OF_DAY) - c_start.get(Calendar.HOUR_OF_DAY);
		return Math.abs(result);
	}
	
	public static String dateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm z");
		String result = sdf.format(date);
		return result;
	}
	
	public static String dateFormatF(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		String result = sdf.format(date);
		return result;
	}
	//2013-08-08
	public static String dateFormatX(String date) {
		String result = "";
		if(date.indexOf("-")!=-1){
			String[] d = date.split("-");
			if(d.length>2){
				result = d[1] + "/" + d[2] + "/" + d[0];
			}
		}
		return result;
	}
	
	public static void main(String[] args) throws ParseException {
		String str = "3A4C";
		int index = str.indexOf("A");
		String count =str.substring(0,index);
		System.out.println(count);
	}
	
	/**
	 * 
	  * @Description: 获取当前时间前30天的时间
	  * @return Date
	  * @Created:lining 2013年11月26日下午12:38:17
	  * @Modified:
	 */
	public static Date getBefore() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getNow());
		calendar.add(Calendar.DAY_OF_MONTH, -30);
		calendar.getTime();
		return calendar.getTime();
	}
	
	/**
	  * <p>(适用条件[可选])
	  * <p>(执行流程[可选])
	  * <p>(使用方法[可选])
	  * <p>(注意事项[可选])
	  *
	  * @Description: 获取指定年月的最后一天的日期 
	  * @param yearMonth指定年月，格式为  yyyy-MM
	  * @return
	  * @throws ParseException
	  * @Created:zhouzj 2013-11-28下午2:38:53
	  * @Modified:
	 */
	public static Date getMonthLastDay(String yearMonth) throws ParseException{
		Calendar calendar = Calendar.getInstance();
		Date date = parseDate(yearMonth, "yyyy-MM");
		calendar.setTime(date);
		
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.roll(Calendar.DAY_OF_MONTH, -1);
		
		return calendar.getTime();
	}
}
